<template>
  <view class="container">
    <uni-list>
      <uni-list-item title="头像" :clickable="true" @click="handleAvatarClick">
        <template v-slot:footer>
          <view class="flex-row">
            <u-avatar :src="avatar" mode="square"></u-avatar>
            <text>></text>
          </view>
        </template>
      </uni-list-item>
      <uni-list-item
        title="昵称"
        :rightText="user.nickName + ' >'"
        :clickable="true"
        @click="handleNickNameClick"
      />
      <uni-list-item
        title="手机号码"
        :rightText="user.phonenumber + '>'"
        :clickable="true"
        @click="handlePhoneClick"
      />
      <uni-list-item
          v-if="userType == '2'"
          title="律师信息"
          :rightText=" '去编辑>'"
          :clickable="true"
          @click="handleEditLawyer"
      />
      <uni-list-item
        title="微信绑定"
        :rightText="!bindWechat ? '已绑定' : '去绑定 >'"
        @click="handleWechatClick"
      />
    </uni-list>
  </view>
</template>

<script>
import { isEmptyString } from "../../../utils/common";
import config from "@/config";
import { getStorage, setStorage, getToken } from "../../../utils/auth";
import application from "@/static/const/application";

const baseUrl = config.baseUrl;

export default {
  data() {
    return {
      user: {},
      roleGroup: "",
      postGroup: "",
      bindWechat: false,
      id: "",
      userType: "1",
    };
  },
  onLoad() {
    this.id = getStorage(application.user).userId;
    const that = this;
    if(!isEmptyString(this.id)){
      this.userType = getStorage(application.user).userType;
    }
    uni.$on("updateUser", function (data) {
      if (data.msg == "页面更新") {
        that.getUser();
      }
    });
    this.getUser();
  },
  computed: {
    avatar() {
      return this.$store.state.user.avatar;
    },
  },
  methods: {
    getUser() {
      if (isEmptyString(this.id)) {
        this.num = "";
        return;
      }
      this.$store.dispatch("GetUserInfoByUserId", this.id).then((response) => {
        this.user = response.data;
        setStorage(application.user, this.user);
        if (!isEmptyString(this.user.openId)) {
          this.bindWechat = true;
        }
      });
    },
    handleAvatarClick() {
      uni.navigateTo({
        url: "/pages/mine/avatar/index",
      });
    },
    handleNickNameClick() {
      if (isEmptyString(this.user.nickName)) {
        return;
      }
      uni.navigateTo({
        url: "/pages/mine/nickName/index",
      });
    },
    handlePhoneClick() {
      if (isEmptyString(this.user.phonenumber)) {
        uni.navigateTo({
          url: "/pages/mine/info/bindPhone",
        });
        return;
      } else {
        uni.navigateTo({
          url: "/pages/mine/editPhone/index",
        });
      }
    },
    //微信绑定
    handleWechatClick() {
      if (!this.bindWechat) {
        return;
      }
      uni.navigateTo({
        url: "/pages/login?state=bindWechat",
      });
    },
    handleEditLawyer() {
      uni.navigateTo({
        url: "/pages/mine/lawyerInfo/index",
      });
    },
  },
};
</script>

<style lang="scss" scoped>
page {
  background-color: #ffffff;
  .flex-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    text {
      margin-left: 10rpx;
    }
  }
  .poup-container {
    width: 50%;
    height: 20rem;
    padding: 20rpx;
    border-radius: 10px;
    text-align: center;
  }
}
</style>
